﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using Model;
using BLL;
using System.IO;
using System.Data;

public partial class VehicleUI_Base_VehicleAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindType();
            BindObjectGroup();
            if (Request.QueryString["Type"] == null)
            { }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_FrameNO.Enabled = false;
                txt_ID.Enabled = false;
                txt_Price.Enabled = false;
                txt_VehicleID.Enabled = false;
                ddl_KeepPerson.Enabled = false;
                ddl_BackupKeyPerson.Enabled = false;
                ddl_ChargePerson.Enabled = false;
                ddl_SuperChargePerson.Enabled = false;
                txt_StartUseDate.Enabled = false;
                txt_PlanScrapDate.Enabled = false;
                ddl_ObjectGroup.Enabled = false;
                ddl_Type.Enabled = false;
                chk_IsPrivate.Enabled = false;


                ful_CHK.Visible = false;
                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void BindType()
    {
        DataTable dt = new VehicleBLL().GetVehicleType();
        ddl_Type.DataSource = dt;
        ddl_Type.DataValueField = "TypeID";
        ddl_Type.DataTextField = "TypeName";
        ddl_Type.DataBind();
    }

    protected void BindObjectGroup()
    {
        DataSet ds = new ObjectGroupBLL().GetOrganList((int)Session["OrganID"]);
        if (ds != null)
        {
            ddl_ObjectGroup.DataSource = ds.Tables[0];
            ddl_ObjectGroup.DataValueField = "Code";
            ddl_ObjectGroup.DataTextField = "Name";
            ddl_ObjectGroup.DataBind();
        }
    }

    protected void LoadService()
    {
        string _id = "";
        if (Session["FilterFrameNO"] != null)
        {
            _id = Session["FilterFrameNO"].ToString();
        }
        else
        {
            _id = Request.QueryString["FrameNO"].ToString();
        }
        Vehicle cs = new VehicleBLL().GetModel(_id);
        if (cs != null)
        {
            txt_FrameNO.Text = cs.FrameNO;
            txt_ID.Text = cs.ID.ToString();
            txt_Price.Text = cs.VehiclePrice.ToString();
            txt_VehicleID.Text = cs.VehicleID;
            ddl_KeepPerson.SelectedValue = cs.KeepPerson;
            ddl_BackupKeyPerson.SelectedValue = cs.BackupKeyPerson;
            ddl_ChargePerson.SelectedValue = cs.ChargePerson;
            ddl_SuperChargePerson.SelectedValue = cs.SuperChargePerson;
            txt_StartUseDate.Text = cs.StartUseDate.ToString();
            txt_PlanScrapDate.Text = cs.PlanScrapDate.ToString();
            ddl_ObjectGroup.SelectedValue = cs.ObjectGroup;
            ddl_Type.SelectedValue = cs.VehicleType.ToString();
            chk_IsPrivate.Checked = cs.IsPrivate;
            if (cs.Photo != string.Empty)
            {
                img_CHK.ImageUrl = GlobalArgs.VehiclePath + cs.Photo;
            }
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        if (!ValidateService.IsNumber(txt_Price.Text))
        {
            UtilityService.Alert(this.Page,"购买价格格式错误!");
            txt_Price.Focus();
            return;
        }


        if (Request.QueryString["Type"] == null)
        {
            Vehicle cs = new Vehicle();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.VehiclePrice = decimal.Parse(txt_Price.Text.Trim());
            cs.VehicleID = txt_VehicleID.Text.Trim();
            cs.KeepPerson = ddl_KeepPerson.SelectedValue.Trim();
            cs.BackupKeyPerson = ddl_BackupKeyPerson.SelectedValue.Trim();
            cs.ChargePerson = ddl_ChargePerson.SelectedValue.Trim();
            cs.SuperChargePerson = ddl_SuperChargePerson.SelectedValue.Trim();
            cs.StartUseDate = string.IsNullOrEmpty(txt_StartUseDate.Text)?DateTime.Parse("1900-1-1"):DateTime.Parse(txt_StartUseDate.Text.Trim());
            cs.PlanScrapDate =DateTime.Parse(txt_PlanScrapDate.Text.Trim());
            cs.ObjectGroup = ddl_ObjectGroup.SelectedValue;
            cs.VehicleType = Convert.ToInt32(ddl_Type.SelectedValue);
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();
            cs.IsPrivate = chk_IsPrivate.Checked;

            string f2 = UploadFile(ful_CHK, 1);

            cs.Photo = f2.Length > 5 ? f2 : "";

            int re = new VehicleBLL().Add(cs);

            if (re>0)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "头像上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "VehicleMgr.aspx");
            }
            else
            {
                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.VehiclePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }
                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            string _id = Request.QueryString["FrameNO"].ToString();
            Vehicle cs = new VehicleBLL().GetModel(_id);
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.VehiclePrice = decimal.Parse(txt_Price.Text.Trim());
            cs.VehicleID = txt_VehicleID.Text.Trim();
            cs.KeepPerson = ddl_KeepPerson.SelectedValue.Trim();
            cs.BackupKeyPerson = ddl_BackupKeyPerson.SelectedValue.Trim();
            cs.ChargePerson = ddl_ChargePerson.SelectedValue.Trim();
            cs.SuperChargePerson = ddl_SuperChargePerson.SelectedValue.Trim();
            cs.StartUseDate = string.IsNullOrEmpty(txt_StartUseDate.Text) ? DateTime.Parse("1900-1-1") : DateTime.Parse(txt_StartUseDate.Text.Trim());
            cs.PlanScrapDate = DateTime.Parse(txt_PlanScrapDate.Text.Trim());
            cs.ObjectGroup = ddl_ObjectGroup.SelectedValue;
            cs.VehicleType = Convert.ToInt32(ddl_Type.SelectedValue);
            cs.IsPrivate = chk_IsPrivate.Checked;


            string f2 = "";
            if (ful_CHK.HasFile)
            {
                f2 = UploadFile(ful_CHK, 1);
                cs.Photo = f2.Length > 5 ? f2 : "";
            }

            int re = new VehicleBLL().Update(cs);

            if (re>0)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "检查图片上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "VehicleMgr.aspx");
            }
            else
            {

                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.VehiclePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }

                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    public string UploadFile(FileUpload ful, int type)
    {

        try
        {

            if (!ful.HasFile)
            {
                // this.Upload_info.Text = "请选择上传文件！";
                return "none";
            }
            else
            {
                string filepath = ful.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\My Pictures\20022775_m.jpg 
                string filename = "";
                string ext = filepath.Substring(filepath.LastIndexOf("."));
                filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_vehicle" + ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                //if (type == 0)
                //{

                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_gps" + ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                //}
                //else
                //{
                //    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_chk" + ext;
                //}

                string serverpath = Server.MapPath(GlobalArgs.VehiclePath) + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

                ful.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                //this.Upload_info.Text = "上传成功！";
                return filename;
            }
        }
        catch (Exception ex)
        {
            UtilityService.Alert(this.Page, "上传发生错误!原因是:" + ex.ToString());
            //this.Upload_info.Text = "上传发生错误！原因是：" + ex.ToString();
            return "err";
        }

    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        UtilityService.Open(this.Page, "../../ImageView.aspx?url=" + ((ImageButton)sender).ImageUrl);
    }
}
